package com.jike.noobmoney.mvp.view.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxEvent;
import com.jike.noobmoney.mvp.view.widget.ProgressDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by kekex on 2018/8/21.
 * fragment的基类
 */

public abstract class BaseFragment extends Fragment {

    protected Context context;
    protected LayoutInflater layoutInflater;
    Unbinder unbinder;
    private ProgressDialog progressDialog;
    private Subscription subscribe;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        if (arguments != null) {
            handlerArguments(arguments);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(layoutResID(), container, false);
        unbinder = ButterKnife.bind(this, view);

        progressDialog = ProgressDialog.createDialog(getActivity());
        progressDialog.setCancelable(isCancel());
        progressDialog.setCanceledOnTouchOutside(isCancel());

        if (isRegistRxBus()) {
            subscribe = RxBus.getInstance().toObservable(RxEvent.class)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(rxEvent -> {
                        if (rxEvent != null) {
                            receiveEvent(rxEvent);
                        }
                    });
            RxBus.getInstance().addSubscription(this, subscribe);
        }

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();
        layoutInflater = LayoutInflater.from(context);
        initData();
    }

    /**
     * 是否在activity中注册订阅事件
     */
    protected boolean isRegistRxBus() {
        return false;
    }

    /**
     * 注册订阅事件之后重写该方法
     */
    protected void receiveEvent(RxEvent rxEvent) {
    }



    /**
     * 设置Fragment布局Id
     */
    protected abstract int layoutResID();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 处理从Activity传过来的参数
     */
    protected void handlerArguments(Bundle arguments) {

    }

    protected void showProgressDialog() {
        Activity activity = getActivity();
        if (context != null && null != activity && !activity.isFinishing() && null != progressDialog) {
            if (!progressDialog.isShowing()) {
                progressDialog.show();
            }
        }
    }

    protected void dismissProgressDialog() {
        if (context != null && !((Activity) context).isFinishing() && null != progressDialog) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }

    /**
     * 是否取消弹框
     *
     * @return
     */
    protected boolean isCancel() {
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (progressDialog!=null){
            progressDialog.dismiss();
        }
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
